/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.elem;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import self.gee.IGraphicElement;
import self.lang.BooleanUtils;
import self.xml.DomUtils;

public class ScrollbarView extends ARectangularHandledElement {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  final static public String TYPE_PROP = "horizType";

  final static public boolean HORIZ = true;
  final static public boolean VERT = false;
  
  private boolean type = HORIZ;
  
  public ScrollbarView() {
    fig.setBounds( 10, 10, 300, 16 );
    resetFrame();
//    PainterManager.registerOwner( this );
//    textPainter = (TextPainter) PainterManager.getPainterForOwner( this, TextPainter.class );
  }
  
  // IEditableElement support
  public void       getProperties( Map store ) {
    store.put( TYPE_PROP, BooleanUtils.toBoolean(type) );
  }
  
  public void       setProperties( Map data ) {
    type = BooleanUtils.toPrimitive( data.get(TYPE_PROP) );
    layer.setDirty();
  }
  
  // IPersistentElement support  
  public void readFrom( Node self, HashMap idObjLookUp ) throws DOMException {
    super.readFrom( self, idObjLookUp );
    type = BooleanUtils.toValue(DomUtils.getElementAttribute(self, TYPE_PROP) );
  }
  
  public void writeTo( Document doc, Element self, HashMap objIdLookup ) throws DOMException {
    super.writeTo( doc, self, objIdLookup );
    DomUtils.setElementAttribute( self, TYPE_PROP, BooleanUtils.toString(type) );
  }  
  
  public void       resize( int x, int y, Point[] handle, MouseEvent e, int dragState ) {
    super.resize( x, y, handle, e, dragState );
    if ( dragState == IGraphicElement.DS_AFTER_LAST ) 
      type = fig.width >= fig.height;
  }
  
  public void       paint( Graphics surface ) {
    if ( outlineOnly ) {
      surface.setColor( frameColor );
      surface.drawRect( fig.x, fig.y, fig.width, fig.height );
      return;
    }
    int x2 = fig.x + fig.width;
    int y2 = fig.y + fig.height;
        
    paintScroller( surface, fig.x, fig.y, x2, y2, type );
  }
  
  final static public int BUTTON_THICKNESS = 17;
  static public void paintScroller( Graphics g, int x1, int y1, int x2, int y2, boolean horiz ) {
    int width = x2 - x1;
    int height = y2 - y1;
    g.setColor( Color.gray );
    g.fillRect( x1, y1, width, height );
    
    g.setColor( Color.black );
    g.drawRect( x1, y1, width, height );
    
    int b1x1, b1x2, b1y1, b1y2;
    int b2x1, b2x2, b2y1, b2y2;
    if ( horiz ) {
      b1x1 = x1; b1x2 = b1x1 + BUTTON_THICKNESS;
      b1y1 = y1; b1y2 = y2;
      b2x1 = x2 - BUTTON_THICKNESS; b2x2 = x2;
      b2y1 = y1; b2y2 = y2;
      paintScrollerButton( g, b1x1, b1y1, b1x2, b1y2, 3 );
      paintScrollerButton( g, b2x1, b2y1, b2x2, b2y2, 1 );
    } else {
      b1x1 = x1; b1x2 = x2; 
      b1y1 = y1; b1y2 = y1 + BUTTON_THICKNESS;
      b2x1 = x1; b2x2 = x2;
      b2y1 = y2 - BUTTON_THICKNESS; b2y2 = y2;
      paintScrollerButton( g, b1x1, b1y1, b1x2, b1y2, 0 );
      paintScrollerButton( g, b2x1, b2y1, b2x2, b2y2, 2 );
    }
  }
  
  final static private int INSIDE = 4;
  static public void paintScrollerButton( Graphics g, int x1, int y1, int x2, int y2, int dir ) {
    g.setColor( Color.lightGray );
    g.fillRect( x1, y1, x2-x1, y2-y1 );
    int[] xPoints = new int[3], yPoints = new int[3];
    switch ( dir ) {
      case 0: 
        xPoints[0] = x1 + INSIDE;
        xPoints[1] = x1 + ((x2 - x1) / 2);
        xPoints[2] = x2 - INSIDE;
        yPoints[0] = y2 - INSIDE;
        yPoints[1] = y1 + INSIDE;
        yPoints[2] = y2 - INSIDE;
        break;
      case 1: 
        xPoints[0] = x1 + INSIDE;
        xPoints[1] = x2 - INSIDE;
        xPoints[2] = x1 + INSIDE;
        yPoints[0] = y1 + INSIDE;
        yPoints[1] = y1 + ((y2 - y1) / 2);
        yPoints[2] = y2 - INSIDE;
        break;
      case 2: 
        xPoints[0] = x1 + INSIDE;
        xPoints[1] = x1 + ((x2 - x1) / 2);
        xPoints[2] = x2 - INSIDE;
        yPoints[0] = y1 + INSIDE;
        yPoints[1] = y2 - INSIDE;
        yPoints[2] = y1 + INSIDE;
        break;
      case 3: 
        xPoints[0] = x1 + INSIDE;
        xPoints[1] = x2 - INSIDE;
        xPoints[2] = x2 - INSIDE;
        yPoints[0] = y1 + ((y2 - y1) / 2);
        yPoints[1] = y1 + INSIDE;
        yPoints[2] = y2 - INSIDE;
        break;
    }
    g.setColor( Color.black );
    g.fillPolygon( xPoints, yPoints, 3 );
  }
}